package com.qf.shop.service.Impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.shop.entity.Cart;
import com.qf.shop.entity.Goods;
import com.qf.shop.mapper.CartNumMapper;
import com.qf.shop.mapper.GoodsMapper;
import com.qf.shop.model.TableData;
import com.qf.shop.service.IGoodsService;
import com.qf.shop.vo.GoodsId;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class GoodsServiceImpl implements IGoodsService {

    private GoodsMapper goodsMapper;
    private CartNumMapper cartNumMapper;

    public GoodsServiceImpl(GoodsMapper goodsMapper, CartNumMapper cartNumMapper) {

        this.goodsMapper = goodsMapper;
        this. cartNumMapper =cartNumMapper;
    }

    @Override
    public TableData<Goods> getAllGoods(Integer offset, Integer limit) {
        Page<Goods> page = PageHelper.offsetPage(offset, limit);
        goodsMapper.selectList(Wrappers.emptyWrapper());

        TableData<Goods> tableData = new TableData<>();
        tableData.setRows(page.getResult());
        tableData.setTotal(page.getTotal());
        return tableData;
    }

    @Override
    public List<Goods> getAllGoods() {
        List<Goods> goods = goodsMapper.selectList(Wrappers.emptyWrapper());
        return goods;
    }

    @Override
    public void deleteGoodsById(Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus("0");
        goodsMapper.updateById(goods);
    }

    @Override
    public void addGoods(Goods goods) {
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setStatus("1");
        GoodsId goodsId = goodsMapper.getGoodsId();
        goods.setGoodId(goodsId.getGoodsId()+1);

        goodsMapper.insert(goods);
    }

    @Override
    public List<Cart> showCart(Integer id) {
        return cartNumMapper.showCart(id);
    }



    @Override
    public void batchInset(List<Goods> userList) {
         goodsMapper.batchGoodsInsert(userList);
    }
}
